<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/14
 * Time: 00:26
 */

namespace CK\Util\IO;

use CK\Core\Base;

class Directory extends Base{
    /**
     * @var array File
     */
    private static $_instance=[];

    /**
     * @param $dir_name
     * 初始化当前类实例
     * @return mixed
     */
    public static function inst($dir_name=null) {
        if (isset(self::$_instance[$dir_name])) {
            return self::$_instance[$dir_name];
        } else {
            self::$_instance[$dir_name] = new self($dir_name);
            return self::$_instance[$dir_name];
        }
    }

    public static function Create($dir_path) {
        $dir_path = self::RP($dir_path);
        $path_arr = explode("/",$dir_path);
        $tmp_dir = array_shift($path_arr);
        $tmp_dir .= "/";
        foreach ($path_arr as $value) {
            if ($value == "." || $value == "..") continue;

            $tmp_dir .= $value;

            if (!self::Exists($tmp_dir)) {
                $flag = @mkdir($tmp_dir);
            }
            $tmp_dir .= "/";
        }
    }

    public static function Clear($dir_path,$is_me=false) {
        $dir_deleted = 0;
        if(!$dh = @opendir($dir_path)) return;
        while (($obj = readdir($dh))) {
            if($obj=='.' || $obj=='..') continue;
            if (!@unlink($dir_path.'/'.$obj)) {
                self::Clear($dir_path.'/'.$obj);
            } else {
                $dir_deleted++;
            }
        }
        if ($is_me) {
            @rmdir($dir_path);
        }
        closedir($dh);
    }

    public static function Exists($dir_path) {
        return is_dir($dir_path);
    }

    public static function RP($dir_path) {
        $dir_path = str_replace("\\","/",$dir_path);
        $dir_path = str_replace("//","/",$dir_path);
        $path_arr = explode("/",$dir_path);
        $path_new = [];
        foreach ($path_arr as $node) {
            if ($node == "..") {
                array_pop($path_new);
            } elseif ($node == ".") {

            } else {
                $path_new[] = $node;
            }
        }
        return implode("/",$path_new);
    }

    public static function getDir($path,$type="file") {
        $tmp_arr = [];
        if(!$dh = @opendir($path)) return [];
        if ($type == "file") {
            while (($obj = readdir($dh))) {
                if($obj=='.' || $obj=='..') continue;
                if (!is_dir($path.'/'.$obj)) {
                    $tmp_arr[] = $obj;
                }
            }
        } elseif ($type == "dir") {
            while (($obj = readdir($dh))) {
                if($obj=='.' || $obj=='..') continue;
                if (is_dir($path.'/'.$obj)) {
                    $tmp_arr[] = $obj;
                }
            }
        } else {
            while (($obj = readdir($dh))) {
                if($obj=='.' || $obj=='..') continue;
                //echo $obj."</br>";
                $tmp_arr[] = $obj;
            }
        }
        closedir($dh);
        return $tmp_arr;
    }
}